package cn.ondraw.ragflow.service.impl;

import cn.ondraw.ragflow.pojo.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.service.RagFlowFileService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 文件服务实现类
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class RagFlowFileServiceImpl implements RagFlowFileService {

    private final PythonApiCaller pythonApiCaller;

    private final String URL_PREFIX = "/file";

    /**
     * 上传文件
     *
     * @param request 文件上传请求对象
     * @return 响应实体
     */
    @Override
    public ResponseEntity<?> upload(FileUploadRequest request) {
        return pythonApiCaller.callPostApi(URL_PREFIX + "/upload", request);
    }

    /**
     * 创建文件
     *
     * @param request 文件创建请求对象
     * @return 响应实体
     */
    @Override
    public ResponseEntity<?> create(FileCreateRequest request) {
        return pythonApiCaller.callPostApi(URL_PREFIX + "/create", request);
    }

    /**
     * 列出文件列表
     *
     * @param request 文件列表请求对象
     * @return 响应实体
     */
    @Override
    public ResponseEntity<?> list(FileListRequest request) {
        try {
            String url = URL_PREFIX + "/list?" +
                "parent_id=" + request.getParent_id() +
                "&keywords=" + URLEncoder.encode(request.getKeywords(), StandardCharsets.UTF_8) +
                "&page=" + request.getPage() +
                "&page_size=" + request.getPage_size() +
                "&orderby=" + request.getOrderby() +
                "&desc=" + request.getDesc();
            return pythonApiCaller.callGetApi(url);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }

    /**
     * 获取根文件夹
     *
     * @return 响应实体
     */
    @Override
    public ResponseEntity<?> getRootFolder() {
        return pythonApiCaller.callGetApi(URL_PREFIX + "/root_folder");
    }

    /**
     * 获取父文件夹
     *
     * @param fileId 文件 ID
     * @return 响应实体
     */
    @Override
    public ResponseEntity<?> getParentFolder(String fileId) {
        return pythonApiCaller.callGetApi(URL_PREFIX + "/parent_folder?file_id=" + fileId);
    }

    /**
     * 获取所有父文件夹
     *
     * @param fileId 文件 ID
     * @return 响应实体
     */
    @Override
    public ResponseEntity<?> getAllParentFolders(String fileId) {
        return pythonApiCaller.callGetApi(URL_PREFIX + "/all_parent_folder?file_id=" + fileId);
    }

    /**
     * 删除文件
     *
     * @param request 文件删除请求对象
     * @return 响应实体
     */
    @Override
    public ResponseEntity<?> rm(FileRmRequest request) {
        return pythonApiCaller.callPostApi(URL_PREFIX + "/rm", request);
    }

    /**
     * 重命名文件
     *
     * @param request 文件重命名请求对象
     * @return 响应实体
     */
    @Override
    public ResponseEntity<?> rename(FileRenameRequest request) {
        return pythonApiCaller.callPostApi(URL_PREFIX + "/rename", request);
    }

    /**
     * 获取文件
     *
     * @param fileId 文件 ID
     * @return 响应实体
     */
    @Override
    public ResponseEntity<?> get(String fileId) {
        return pythonApiCaller.callGetApi(URL_PREFIX + "/get/" + fileId);
    }

    /**
     * 移动文件
     *
     * @param request 文件移动请求对象
     * @return 响应实体
     */
    @Override
    public ResponseEntity<?> move(MoveRequest request) {
        return pythonApiCaller.callPostApi(URL_PREFIX + "/mv", request);
    }
}
